class Sqlc < Formula
  desc "Generate type safe Go from SQL"
  homepage "https://sqlc.dev/"
  url "https://github.com/kyleconroy/sqlc/archive/v1.9.0.tar.gz"
  sha256 "8cb54ade4125602ea7adeb783efdd73de698c62c67b3e8ab4bd60371f425f30d"
  license "MIT"
  head "https://github.com/kyleconroy/sqlc.git", branch: "main"

  bottle do
    sha256 cellar: :any_skip_relocation, arm64_big_sur: "f8ddf6155a3da14592bc4495e2a886b43618130000f4df8e6bb7ba7da307219c"
    sha256 cellar: :any_skip_relocation, big_sur:       "c679692541c7354727131f04710df15a2b5e9c40ec983e323bb9a99d7b5e2157"
    sha256 cellar: :any_skip_relocation, catalina:      "d606bfb712742f046e1b1f0ce662f1ce1fda4a16823abff61661d3a7b01a1aa3"
    sha256 cellar: :any_skip_relocation, mojave:        "d7e58abb333731d563686e499a6c0e748e377fb8a08872201ab7622d358e7de0"
    sha256 cellar: :any_skip_relocation, x86_64_linux:  "b5a6764c6d252b96fa7edc2eac3985da3aeca140449f36e70d94626ce254fb1f"
  end

  depends_on "go" => :build

  def install
    system "go", "build", *std_go_args, "-ldflags", "-s -w", "./cmd/sqlc"
  end

  test do
    (testpath/"sqlc.json").write <<~SQLC
      {
        "version": "1",
        "packages": [
          {
            "name": "db",
            "path": ".",
            "queries": "query.sql",
            "schema": "query.sql",
            "engine": "postgresql"
          }
        ]
      }
    SQLC

    (testpath/"query.sql").write <<~EOS
      CREATE TABLE foo (bar text);

      -- name: SelectFoo :many
      SELECT * FROM foo;
    EOS

    system bin/"sqlc", "generate"
    assert_predicate testpath/"db.go", :exist?
    assert_predicate testpath/"models.go", :exist?
    assert_match "// Code generated by sqlc. DO NOT EDIT.", File.read(testpath/"query.sql.go")
  end
end
